'''
封装requests中get，post方法
1、增加异常处理
2、创建一个session，用session发送请求，保持状态
3、打印日志，方便定位问题
'''
import requests


class BaseRequests:
    def __init__(self):
        # 实例化方法
        self.session = requests.Session()

    def get(self,url,**kwargs):
        try:
            print(f"发送get请求，url为：{url},参数为：{kwargs}")
            r = self.session.get(url,**kwargs)
            print(f"发送get请求成功，响应为：{r.text}")
            return r
        except Exception as e:
            print(f"发送get请求异常，异常信息为：{e}")


    def post(self,url,**kwargs):
        try:
            print(f"发送post请求，url为：{url},参数为：{kwargs}")
            r = self.session.post(url,**kwargs)
            print(f"发送post请求成功，响应为：{r.text}")
            return r
        except Exception as e:
            print(f"发送post请求异常，异常信息为：{e}")

 # 测试代码
if __name__ == '__main__':
    br = BaseRequests()
    cs =  {"mobilephone":"15190384956","pwd":"12345","regname":"草莓熊"}
    url = "http://192.168.0.103:8088/futureloan/mvc/api/member/register"
    br.get(url,params=cs)
    br.post(url,data=cs)


